home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / atre27.exe / ATREE_27 / OCRDEMO2 / EDITSCRO.CPP < prev    next >
C/C++ Source or Header  |  1992-08-01  |  13KB  |  456 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** editscro.cpp                                                        ****
  4.  ****                                                                     ****
  5.  **** atree release 2.7 for Windows                                       ****
  6.  **** Adaptive Logic Network (ALN) simulation program.                    ****
  7.  **** Copyright (C) M. Thomas, N. Sanche, W.W. Armstrong 1991, 1992       ****
  8.  ****                                                               ****
  9.  **** License:                                                            ****
  10.  **** A royalty-free license is granted for the use of this software for  ****
  11.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and/or     ****
  12.  **** modified provided this notice appears in its entirety and unchanged ****
  13.  **** in all derived source programs.  Persons modifying the code are     ****
  14.  **** requested to state the date, the changes made and who made them     ****
  15.  **** in the modification history.                                        ****
  16.  ****                                                                     ****
  17.  **** Patent License:                                                     ****
  18.  **** The use of a digital circuit which transmits a signal indicating    ****
  19.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  20.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  21.  **** W. W. Armstrong, President.  A royalty-free license is granted      ****
  22.  **** by the company to use this patent for NON_COMMERCIAL PURPOSES to    ****
  23.  **** adapt logic trees using this program and its modifications.         ****
  24.  ****                                                                     ****
  25.  **** Limited Warranty:                                                   ****
  26.  **** This software is provided "as is" without warranty of any kind,     ****
  27.  **** either expressed or implied, including, but not limited to, the     ****
  28.  **** implied warrantees of merchantability and fitness for a particular  ****
  29.  **** purpose.  The entire risk as to the quality and performance of the  ****
  30.  **** program is with the user.  Neither the authors, nor the             ****
  31.  **** University of Alberta, its officers, agents, servants or employees  ****
  32.  **** shall be liable or responsible in any way for any damage to         ****
  33.  **** property or direct personal or consequential injury of any nature   ****
  34.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  35.  **** or any other party as a consequence of the use or disposition of    ****
  36.  **** this software.                                                      ****
  37.  **** Modification history:                                               ****
  38.  ****                                                                     ****
  39.  **** 92.04.27 atree v2.5 for Windows, M. Thomas                          ****
  40.  **** 92.03.07 Release 2.6, Monroe Thomas, Neal Sanche                    ****
  41.  **** 92.01.08 Release 2.7, Monroe Thomas, Neal Sanche                    ****
  42.  ****                                                                     ****
  43.  *****************************************************************************/
  44.  
  45.  
  46. #ifndef __EDITSCRO_H
  47. #include "editscro.h"
  48. #endif
  49.  
  50. #ifndef __WINDOWSX_H
  51. #include "windowsx.h"
  52. #endif
  53.  
  54. #define EDITHEIGHT 22
  55.  
  56. const int IDES_EDIT = 100;
  57. const int IDES_UP = 101;
  58. const int IDES_DOWN = 102;
  59. const int IDES_BORDER = 103;
  60. const int ESBN_CLICKED = 3;
  61. const int ESBN_NOMORE = 4;
  62.  
  63. _CLASSDEF(TESButton)
  64.  
  65. class TESButton : public TButton
  66. {
  67. private:
  68.     HBITMAP hbmDown;
  69.     HBITMAP hbmUp;
  70.  
  71.     WORD wSrcHeight;
  72.     WORD wSrcWidth;
  73.  
  74.     BOOL bClicked;
  75.  
  76. public:
  77.     void Init(int AnId);
  78.  
  79.     TESButton(PTWindowsObject AParent, int AnId, LPSTR AText,
  80.                         int X, int Y, int W, int H, PTModule AModule = NULL) :
  81.         TButton(AParent, AnId, AText, X, Y, W, H, FALSE, AModule)
  82.         { DisableAutoCreate(); Init(AnId); }
  83.  
  84.     ~TESButton()
  85.       { DeleteBitmap(hbmDown); DeleteBitmap(hbmUp); }
  86.  
  87.     virtual void ODADrawEntire(DRAWITEMSTRUCT& dis);
  88.     virtual void ODASelect(DRAWITEMSTRUCT& dis);
  89.  
  90.     virtual void WMLButtonUp(RTMessage) = [WM_FIRST + WM_LBUTTONUP]
  91.         {    SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  92.                                     MAKELPARAM(HWindow, ESBN_NOMORE)); }
  93.     virtual void WMLButtonDblClk(RTMessage Msg) = [WM_FIRST + WM_LBUTTONDBLCLK]
  94.         { SendMessage(HWindow, WM_LBUTTONDOWN, Msg.WParam, Msg.LParam); }
  95.     virtual void WMEnable(RTMessage Msg) = [WM_FIRST + WM_ENABLE];
  96. };
  97.  
  98. void
  99. TESButton::Init(int AnId)
  100. {
  101.     DisableAutoCreate();
  102.     Attr.Style |= BS_OWNERDRAW;
  103.     if (AnId == IDES_UP)
  104.     {
  105.         hbmUp = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_UPARROW));
  106.         hbmDown = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_UPARROWD));
  107.     }
  108.     else
  109.     {
  110.         hbmUp = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_DNARROW));
  111.         hbmDown = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_DNARROWD));
  112.     }
  113.  
  114.     BITMAP bm;
  115.  
  116.     GetObject(hbmUp, sizeof(bm), (LPSTR)&bm);
  117.     wSrcHeight = bm.bmHeight;
  118.     wSrcWidth = bm.bmWidth;
  119. }
  120.  
  121. void
  122. TESButton::ODADrawEntire(DRAWITEMSTRUCT& dis)
  123. {
  124.     HDC  hdcMem;
  125.  
  126.     RECT rc;
  127.     GetWindowRect(HWindow, &rc);
  128.  
  129.     DPtoLP(dis.hDC, (LPPOINT)&rc, 2);
  130.     int wWidth = rc.right - rc.left;
  131.     int wHeight = rc.bottom - rc.top;
  132.  
  133.     hdcMem = CreateCompatibleDC(dis.hDC);
  134.     SelectObject(hdcMem,(dis.itemState & ODS_SELECTED) ? hbmDown : hbmUp);
  135.     SetStretchBltMode(dis.hDC, BLACKONWHITE);
  136.     StretchBlt(dis.hDC, dis.rcItem.left, dis.rcItem.top, wWidth, wHeight,
  137.                          hdcMem, 0, 0, wSrcWidth, wSrcHeight, SRCCOPY);
  138.  
  139.     DeleteDC(hdcMem);
  140. }
  141.  
  142. void
  143. TESButton::ODASelect(DRAWITEMSTRUCT far & dis)
  144. {
  145.     HDC  hdcMem;
  146.  
  147.     RECT rc;
  148.     GetWindowRect(HWindow, &rc);
  149.  
  150.     DPtoLP(dis.hDC, (LPPOINT)&rc, 2);
  151.     int wWidth = rc.right - rc.left;
  152.     int wHeight = rc.bottom - rc.top;
  153.  
  154.     hdcMem = CreateCompatibleDC(dis.hDC);
  155.     SelectObject(hdcMem,(dis.itemState & ODS_SELECTED) ? hbmDown : hbmUp);
  156.     SetStretchBltMode(dis.hDC, BLACKONWHITE);
  157.     StretchBlt(dis.hDC, dis.rcItem.left, dis.rcItem.top, wWidth, wHeight,
  158.                          hdcMem, 0, 0, wSrcWidth, wSrcHeight, SRCCOPY);
  159.  
  160.     DeleteDC(hdcMem);
  161.  
  162.     if (dis.itemState & ODS_SELECTED)
  163.     {
  164.         bClicked = TRUE;
  165.     }
  166.     else
  167.     {
  168.         bClicked = FALSE;
  169.     }
  170.  
  171.     // enter PeekMessage loop until clicked = FALSE
  172.  
  173.     int wCounter = 0;
  174.     while (bClicked)
  175.     {
  176.         if ((wCounter++ % 250) == 0)
  177.         {
  178.             SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  179.                                     MAKELPARAM(HWindow, ESBN_CLICKED));
  180.         }
  181.  
  182.         MSG msg;
  183.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  184.         {
  185.              TranslateMessage(&msg);
  186.              DispatchMessage(&msg);
  187.         }
  188.     }
  189. }
  190.  
  191. void
  192. TESButton::WMEnable(RTMessage Msg)
  193. {
  194.     if (Attr.Id == IDES_UP)
  195.     {
  196.         if (Msg.WParam)
  197.             hbmUp = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_UPARROW));
  198.         else
  199.             hbmUp = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_UPARROWI));
  200.     }
  201.     else
  202.     {
  203.         if (Msg.WParam)
  204.             hbmUp = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_DNARROW));
  205.         else
  206.             hbmUp = LoadBitmap(NULL,MAKEINTRESOURCE(OBM_DNARROWI));
  207.     }
  208.     InvalidateRect(HWindow, NULL, FALSE);
  209. }
  210.  
  211. class TESEdit : public TEdit
  212. {
  213. public:
  214.     TESEdit(PTWindowsObject AParent, int AnId, LPSTR AText, int X, int Y, int W,
  215.             int H, WORD ATextLen, BOOL Multiline = FALSE,
  216.             PTModule AModule = NULL)
  217.         : TEdit(AParent, AnId, AText, X, Y, W, H, ATextLen, Multiline, AModule)
  218.         { DisableAutoCreate(); }
  219.  
  220.     virtual void WMSetFocus(RTMessage) = [WM_FIRST + WM_SETFOCUS];
  221.     virtual void WMChar(RTMessage) = [WM_FIRST + WM_CHAR];
  222.     virtual void WMKeyDown(RTMessage) = [WM_FIRST + WM_KEYDOWN];
  223. };
  224.  
  225. void
  226. TESEdit::WMSetFocus(RTMessage Msg)
  227. {
  228.     SendMessage(Parent->Parent->HWindow, WM_ACTIVATE, 1, 0L);
  229.     SetSelection(0, TextLen);
  230.     DefWndProc(Msg);
  231. }
  232.  
  233. void
  234. TESEdit::WMChar(RTMessage Msg)
  235. {
  236.     if (((Msg.WParam >= '0') && (Msg.WParam <= '9')) || (Msg.WParam == '-')
  237.         || (Msg.WParam == 8))
  238.     {
  239.         DefWndProc(Msg);
  240.     }
  241. }
  242.  
  243. void
  244. TESEdit::WMKeyDown(RTMessage Msg)
  245. {
  246.     DefWndProc(Msg);
  247.     if (Msg.WParam == VK_UP)
  248.     {
  249.         SendMessage(Parent->HWindow, WM_COMMAND, IDES_UP,
  250.                                 MAKELONG(HWindow, ESBN_CLICKED));
  251.         SetSelection(0, TextLen);
  252.     }
  253.     else if (Msg.WParam == VK_DOWN)
  254.     {
  255.         SendMessage(Parent->HWindow, WM_COMMAND, IDES_DOWN,
  256.                                 MAKELONG(HWindow, ESBN_CLICKED));
  257.         SetSelection(0, TextLen);
  258.     }
  259. }
  260.  
  261. void
  262. TEditScroll::Init(int AStart, int AnEnd, int AFirst,
  263.                                     int ATextLen, PTModule AModule)
  264. {
  265.     wStart = AStart;
  266.     wEnd = AnEnd;
  267.     wCurrent = AFirst;
  268.     wTextLen = ATextLen;
  269.     Attr.Style |= WS_BORDER;
  270.     if (wStart > wEnd)
  271.     {
  272.         int wTmp = wEnd;
  273.         wEnd = wStart;
  274.         wStart = wTmp;
  275.     }
  276.     if (wCurrent < wStart) wCurrent = wStart;
  277.     if (wCurrent > wEnd) wCurrent = wEnd;
  278.     Edit = new TESEdit(this, IDES_EDIT, "", 0, 0, 0, 0,
  279.                                          ATextLen, FALSE, AModule);
  280.     Up = new TESButton(this, IDES_UP, "", 0, 0, 0, 0, AModule);
  281.     Down = new TESButton(this, IDES_DOWN, "", 0, 0, 0, 0,    AModule);
  282. }
  283.  
  284. void
  285. TEditScroll::GetWindowClass(WNDCLASS& AWndClass)
  286. {
  287.     TControl::GetWindowClass(AWndClass);
  288.     AWndClass.hCursor = LoadCursor(NULL, IDC_IBEAM);
  289. }
  290.  
  291. void
  292. TEditScroll::SetupWindow()
  293. {
  294.     TControl::SetupWindow();
  295.  
  296.     POINT pt[2];
  297.     GetWindowRect(HWindow, (RECT*)pt);
  298.     ScreenToClient(HWindow, &(pt[0]));
  299.     ScreenToClient(HWindow, &(pt[1]));
  300.  
  301.     Up->Attr.X = pt[1].x - GetSystemMetrics(SM_CXVSCROLL),
  302.     Up->Attr.Y = pt[0].y,
  303.     Up->Attr.W = GetSystemMetrics(SM_CXVSCROLL),
  304.     Up->Attr.H = (pt[1].y - pt[0].y) / 2,
  305.  
  306.     Down->Attr.X = pt[1].x - GetSystemMetrics(SM_CXVSCROLL);
  307.     Down->Attr.Y = pt[0].y + ((pt[1].y - pt[0].y) / 2);
  308.     Down->Attr.W = GetSystemMetrics(SM_CXVSCROLL);
  309.     Down->Attr.H = (pt[1].y - pt[0].y) / 2;
  310.  
  311.     Edit->Attr.Style &= ~WS_BORDER;
  312.     Edit->Attr.X = 2;
  313.     Edit->Attr.H = EDITHEIGHT;
  314.     Edit->Attr.Y = (pt[1].y - pt[0].y - EDITHEIGHT) / 2 + 2;
  315.     Edit->Attr.W = pt[1].x - pt[0].x - GetSystemMetrics(SM_CXVSCROLL) - 3;
  316.  
  317.     GetApplication()->MakeWindow(Up);
  318.     GetApplication()->MakeWindow(Down);
  319.     GetApplication()->MakeWindow(Edit);
  320.  
  321.     char sz[80];
  322.     Edit->SetText((LPSTR)itoa(wCurrent, sz, 10));
  323.     SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  324.                             MAKELPARAM(HWindow, ESN_CHANGE));
  325. }
  326.  
  327. void
  328. TEditScroll::DefChildProc(RTMessage Msg)
  329. {
  330.     char sz[80];
  331.     switch(Msg.LP.Hi)
  332.     {
  333.         case ESBN_NOMORE:
  334.             SetFocus(Edit->HWindow);
  335.             break;
  336.         case ESBN_CLICKED:
  337.             if (Msg.WParam == IDES_UP)
  338.             {
  339.                 wCurrent++;
  340.                 if (wCurrent > wEnd) wCurrent = wStart;
  341.             }
  342.             else
  343.             {
  344.                 wCurrent--;
  345.                 if (wCurrent < wStart) wCurrent = wEnd;
  346.             }
  347.             Edit->SetText((LPSTR)itoa(wCurrent, sz, 10));
  348.             SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  349.                                     MAKELPARAM(HWindow, ESN_CHANGE));
  350.             break;
  351.  
  352.         case EN_UPDATE:
  353.             Edit->GetText(sz, wTextLen);
  354.             int wTmp = atoi(sz);
  355.             if (wTmp < wStart)
  356.             {
  357.                 wsprintf(sz, "Please enter a number between %d and %d!", wStart, wEnd);
  358.                 MessageBox(HWindow, sz, NULL, MB_OK | MB_ICONQUESTION);
  359.                 Edit->SetText((LPSTR)itoa(wCurrent, sz, 10));
  360.                 break;
  361.             }
  362.             if (wTmp > wEnd)
  363.             {
  364.                 wsprintf(sz, "Please enter a number between %d and %d!", wStart, wEnd);
  365.                 MessageBox(HWindow, sz, NULL, MB_OK | MB_ICONQUESTION);
  366.                 Edit->SetText((LPSTR)itoa(wCurrent, sz, 10));
  367.                 break;
  368.             }
  369.             wCurrent = wTmp;
  370.             SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  371.                                     MAKELONG(HWindow, ESN_CHANGE));
  372.             break;
  373.  
  374.         default:
  375.             DefWndProc(Msg);
  376.     }
  377. }
  378.  
  379. void
  380. TEditScroll::WMDrawItem(RTMessage Msg)
  381. {
  382.     if(((LPDRAWITEMSTRUCT)(Msg.LParam))->CtlID == IDES_UP)
  383.     {
  384.         switch ( ((LPDRAWITEMSTRUCT)(Msg.LParam))->itemAction )
  385.         {
  386.             case ODA_DRAWENTIRE:
  387.                 Up->ODADrawEntire(*((LPDRAWITEMSTRUCT)(Msg.LParam)));
  388.                 break;
  389.             case ODA_SELECT:
  390.                 Up->ODASelect(*((LPDRAWITEMSTRUCT)(Msg.LParam)));
  391.                 break;
  392.         }
  393.     }
  394.     else
  395.     {
  396.         switch ( ((LPDRAWITEMSTRUCT)(Msg.LParam))->itemAction )
  397.         {
  398.             case ODA_DRAWENTIRE:
  399.                 Down->ODADrawEntire(*((LPDRAWITEMSTRUCT)(Msg.LParam)));
  400.                 break;
  401.             case ODA_SELECT:
  402.                 Down->ODASelect(*((LPDRAWITEMSTRUCT)(Msg.LParam)));
  403.                 break;
  404.         }
  405.     }
  406. }
  407.  
  408. int
  409. TEditScroll::GetText(LPSTR AString, int ATextLen)
  410. {
  411.     return Edit->GetText(AString, ATextLen);
  412. }
  413.  
  414. void
  415. TEditScroll::SetText(LPSTR AString)
  416. {
  417.     Edit->SetText(AString);
  418.     SendMessage(Parent->HWindow, WM_COMMAND, Attr.Id,
  419.                             MAKELONG(HWindow, ESN_CHANGE));
  420. }
  421.  
  422. void
  423. TEditScroll::WMLButtonDown(RTMessage Msg)
  424. {
  425.     SetFocus(Edit->HWindow);
  426.  
  427.     POINT pt;
  428.     pt.x = Msg.LP.Lo;
  429.     pt.y = Msg.LP.Hi;
  430.     HWND hwndChild = ChildWindowFromPoint(HWindow, pt);
  431.     if (hwndChild == Up->HWindow)
  432.     {
  433.         SendMessage(Up->HWindow, WM_LBUTTONDOWN, Msg.WParam, Msg.LParam);
  434.     }
  435.     else if (hwndChild == Down->HWindow)
  436.     {
  437.         SendMessage(Down->HWindow, WM_LBUTTONDOWN, Msg.WParam, Msg.LParam);
  438.     }
  439. }
  440.  
  441. void
  442. TEditScroll::WMEnable(RTMessage Msg)
  443. {
  444.     EnableWindow(Edit->HWindow, Msg.WParam);
  445.     EnableWindow(Up->HWindow, Msg.WParam);
  446.     EnableWindow(Down->HWindow, Msg.WParam);
  447. }
  448.  
  449. void
  450. TEditScroll::WMSetFocus(RTMessage Msg)
  451. {
  452.     DefWndProc(Msg);
  453.     SetFocus(Edit->HWindow);
  454. }
  455.  
  456.